package com.as.struts.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.as.struts.form.Form;

public class BaseAction extends DispatchAction {

	protected boolean isValid(Form form, ActionMapping mapping,
        HttpServletRequest request) {
        boolean status = true; // assume we're valid.
        ActionMessages errors = form.validate(mapping, request);
        if(errors != null && errors.size() > 0) {
            status = false;
            saveErrors(request, errors);
        }
        return status;
    }
	
	protected void setError(String msgkey, HttpServletRequest req) {
		ActionMessages msgs = new ActionMessages();
		msgs.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(msgkey));
		saveErrors(req, msgs);
	}
	
	protected void setMessage(String msgkey, HttpServletRequest req) {
		ActionMessages msgs = new ActionMessages();
		ActionMessage msg = new ActionMessage(msgkey);
		msgs.add(ActionMessages.GLOBAL_MESSAGE, msg);
		saveMessages(req, msgs);
	}
}
